<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>生态瞬时测试</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="favicon.ico">
    <link href="../css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="../css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <!--<link href="../css/plugins/iCheck/custom.css" rel="stylesheet">-->
    <link href="../css/animate.css" rel="stylesheet">
    <link href="../css/style.css?v=4.1.0" rel="stylesheet">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>实验名称/实验者者管理</h5>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-5 m-b-xs"></div>
                        <div class="col-sm-4 m-b-xs"></div>
                        <div class="col-sm-3">
                            <a href="add-manager-item.html" class="btn btn-sm btn-primary">添加实验人员</a>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                            <tr>

                                <th></th>
                                <th>用户名</th>
                                <th>真实姓名</th>
                                <th>生日</th>
                                <th>联系方式</th>

                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="subject-box">

                            </tbody>
                        </table>
                    </div>

                </div>
            </div>
        </div>

    </div>
</div>

<!-- 全局js -->
<script src="../js/jquery.min.js?v=2.1.4"></script>
<script src="../js/bootstrap.min.js?v=3.3.6"></script>
<script src="../js/paging.js"></script>
<!-- iCheck -->
<script src="../js/plugins/iCheck/icheck.min.js"></script>
<script>
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
    });
</script>

<script src="../js/dateChange.js"></script>

<script>
    $(document).ready(function () {
        var testId = sessionStorage.getItem("testId");
        var testName = sessionStorage.getItem("testName");
        $(".ibox-title").eq(0).html(` <h5>${testName}/实验者管理</h5>`);
        // console.log(testId);
        var token = sessionStorage.getItem("token");

        //获取页面数据
        function initData(_data) {
            var token = sessionStorage.getItem("token");
            var num = _data.current;
            var pageSize = _data.size;
            $.ajax({
                type:"GET",
                url:"http://123.206.18.189:8080//experiments/"+testId+"/experiment_administrators",
                data:_data,
                beforeSend: function(xhr){xhr.setRequestHeader('authToken', token);},
                dataType:'json',
                success: function (res) {
                    var html = "";
                    for(var i=0;i<res.data.length;i++){
                        var day = dateChange(res.data[i].birthday);
                        html += `
                        <tr alt="${res.data[i].userId}">
                                    <td></td>
                                    <td>${res.data[i].userName}</td>
                                    <td>${res.data[i].realName}</td>
                                    <td>${day}</td>
                                    <td>${res.data[i].telephone}</td>

                                    <td>
                                        <i class="fa fa-trash-o del" title="删除" style="margin-right: 20px;"></i>
                                    </td>
                                </tr>
                    `
                    }
                    $("#subject-box").html(html);
                    updatePage(res,_data);
                },
                error:function(XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.status);
                    console.log(XMLHttpRequest.readyState);
                    console.log(textStatus);
                },

            });
        }
        initData({current:1,size:10});
        //分页效果
        function updatePage(res,_data) {
            $("#paging").paging({
                pageNo:_data.current,
                totalPage:Math.ceil(res.meta.pagination.total/res.meta.pagination.size),
                callback: function (num) {
                    _data.current = num;
                    initData(_data)
                }
            })
        }

        //删除被试者
        $("#subject-box").on("click",".del",function () {
            var managerId = $(this).parent().parent().attr("alt");
            var r = confirm("是否要删除该被试者！");
            if(r){
                $.ajax({
                    type:"DELETE",
                    url:"http://123.206.18.189:8080/experiments/"+testId+"/experiment_administrators/"+managerId,
                    headers:{
                        authToken:token
                    },
                    complete:function (XMLHttpRequest) {
                        if(XMLHttpRequest.status==200){
                            alert("实验者已删除！");
                            window.location.reload();
                        }else{
                            alert("删除失败！错误信息"+XMLHttpRequest.status)
                        }

                    }
                })
            }

        })
    })
</script>


</body>

</html>
